package com.order.shiro;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

import com.order.model.Account;
import com.order.model.sys.SysUser;

public class ShiroUtils {

	/***获取用户对象基本数据***/
	
	/**获得用户对象*/
	public static SysUser getUser() {
		ShiroPrincipal principal = getPrincipal();
		if(principal != null) return principal.getUser();
		return null;
	}
	
	/**返回当前登录的认证实体ID*/
	public static int getUserId() {
		ShiroPrincipal principal = getPrincipal();
		if(principal != null) return principal.getUserId();
		return 0;
	}
	
	/**获取当前认证实体的登录名称*/
	public static String getUserName() {
		ShiroPrincipal principal = getPrincipal();
		if(principal != null) return principal.getUserName();
		throw new RuntimeException("user's name is null.");
	}
	
	/** 获取当前登录的认证实体*/
	public static ShiroPrincipal getPrincipal() {
		Subject subject = SecurityUtils.getSubject();
		return (ShiroPrincipal)subject.getPrincipal();
	}
	
}
